/* js file for 2d graphic */
//for namespace;
var G2D         ={};
G2D.Static      ={};
G2D.Static.Draw ={};
G2D.Drawable    ={};
G2D.Debug       ={};
G2D.Global      ={};
G2D.Global.CanvasCount = 0;
G2D.Global.Info ="G2DLibByKidd_ChromeOnly!";
//functions 
G2D.EmptyFunction = function(){}
G2D.Debug.Log = function(msg){}   // do nothing for this;
G2D.StaticString = "hello";
G2D.Static.GetCanvasHTML = function(id,width,height)
{
    return '<canvas id="'+id+'" width="' + width + 'px" height="'+height+'px">Your browser does not support the canvas element.</canvas>';
}

G2D.Static.Get2DContext = function(canvasObject)
{
    var ctx = canvasObject.getContext("2d");
    return ctx;
}

G2D.Static.SetContextColor = function(ctx,fillStyle,borderStyle)
{
    ctx.fillStyle = fillStyle;
}

G2D.Static.Draw.DrawRect = function(ctx,x1,y1,x2,y2)
{
    ctx.fillRect(x1,y1,x2,y2);
}

////////////////////////////////////////////////////////////////////////////////

G2D.Drawable.CanvasUnit = function(divid,width,height,bgcolor)
{
    var canvasname        = "G2DCanvas"+G2D.Global.CanvasCount;
    this.parent           = document.getElementById(divid);
    this.parent.innerHTML = G2D.Static.GetCanvasHTML(canvasname,width,height);
    this.canvas           = document.getElementById(canvasname);
    this.ctx              = G2D.Static.Get2DContext(this.canvas);
    this.bgcolor          = (bgcolor==undefined)?"#000000":bgcolor;
    this.w                = width;
    this.h                = height;
}
G2D.Drawable.CanvasUnit.prototype.Clear = function()
{
    this.ctx.clearRect(0,0,this.w,this.h);
}

////////////////////////////////////////////////////////////////////////////////

//创建一个图片. 并且赋予各种方法.
G2D.Drawable.ImgUnit = function(url,canvasunit)
{
    this.pic = new Image();
    this.pic.src = url;
    this.parent  = (canvasunit.ctx==undefined)?canvasunit:canvasunit.ctx;
    this.x       = 0;
    this.y       = 0;
    this.vx      = 0;
    this.vy      = 0;
    this.str     = G2D.StaticString;
}

G2D.Drawable.ImgUnit.prototype.draw = function( x, y)
{
    if (x == undefined ) x=this.x;
    if (y == undefined ) y=this.y;
    this.parent.drawImage(this.pic,x,y);
}

G2D.Drawable.ImgUnit.prototype.moveTo = function(x,y)
{
    this.x = x;
    this.y = y;
}
G2D.Drawable.ImgUnit.prototype.move = function(x,y)
{
    this.x += (x==undefined)?(this.vx):(x);
    this.y += (y==undefined)?(this.vy):(y);
}
G2D.Drawable.ImgUnit.prototype.setSpeed = function(x,y)
{
    this.vx = (x==undefined)?(0):(x);
    this.vy = (y==undefined)?(0):(y);
}

////////////////////////////////////////////////////////////////////////////